//页面缓存对象
var cache = {};

$(function() {
	initShopSelecter();
});

/**
 * 初始化店面选择器
 */
function initShopSelecter() {
	refreshInput($(".serWrapper"));
	$("#shopIsChoosed").removeAttr("checked");
	$("#shopName").attr("disabled", "disabled");

	$("#shopIsChoosed").bind("click", function() {
		var checked = $(this).attr("checked");
		if (checked) {
			$("#shopName").removeAttr("disabled");
		} else {
			$("#shopName").attr("disabled", "disabled");
			$("#shopName").empty();
		}
	});
}

/**
 * 点击查询按钮
 */
function search() {

	var heads = ["customerName", "mp", "memberCardNo", "carTypeName", "license"];
	var ids = [];
	for (var i = 0; i < heads.length; i++) {
		ids[i] = 's_' + heads[i];
	}
	heads.push("shopId");
	ids.push("shopName");
	var data = qmsk.collectionData(heads, ids);

	cache.pagination = new Pagination({
		url : '../customer/find/list',
		data : data,
		success : function(rst) {
			var response = new qmsk.Response(rst);
			//"customerId","memberId","isMember","memberCardNo","shopId",
			//"customerName","license","brandName","carSystemName",
			//"carTypeName","levelName","integralAmount","depositAmount",
			//"cashCouponAmount","couponAmount","discount","status"
			qmsk.dataTable({
				tbodyId : 'customerTbody',
				heads : ["customerId", "customerName", "mp", "license", "brandName", "carSystemName", "carTypeName", "memberCardNo", "levelName", "depositAmount", "shopName", ["customerId", "memberCardNo", "memberId"]],
				response : response,
				sup : ['sup', '共 [index] 条'],
				handler : [[11, detailHandler]]
			});

		},
		pagerHandler : 'pager'
	});

}

function detailHandler(customerId, memberCardNo, memberId, row) {

	var html = '';

	if (qmsk.isEmpty(memberCardNo) || qmsk.isEmpty(memberId)) {
		html = "<input type='button' class='common_btn' value='详情' onClick='toDetail(" + customerId + ")'/>";
	} else {
		html = "<input type='button' class='common_btn' value='详情' onClick='toDetail(" + customerId + ",\"" + memberCardNo + "\"," + memberId + ")'/>";
	}

	html += "<input type='button' class='common_btn' value='入会' onClick='preMemberShip(" + customerId + ")'/>";
	return html;
}

function shopSelecter(self) {
	$(self).blur();
	initSelecter({
		method : 'post',
		url : '../shop/find/shopList',
		heads : ["id", "name"],
		titles : ["店面Id", "店面名称"],
		zindex : 500,
		callBack : function(response, row) {
			var value = response.getValue('id', row);
			var key = response.getValue('name', row);
			var option = $("<option value='" + value + "'>" + key + "</option>");
			$(self).empty().append($(option));
/*			loadMemberLevel(value);
*/		}
	});
}

function openMemberLevelList(id){
	var shopId = $("#shopId").val();
	if(IsEmpty(shopId)){
		artAlert("请关闭弹出层选择会员入会重试！");
		return false
	}
	
	openMemberLevelListPanel(id,shopId)
}

function __memberLevelSelected(memberLevelId, memberLevelName) {
	$("#levelId").append("<option value='" + memberLevelId + "' selected=\"selected\" >" + memberLevelName + "</option>");
	$("#memberLevelListPanel, #memberLevelListPanel_yinying").hide();
	
	var dt= _findInfo('../memberLevel/find/memberLevelList/0/1', {'id':memberLevelId});
	if(IsNotEmpty(dt)&&dt.state==0){
		$("#memberFree").val(_getIntOfJson(0, dt, "MemberFee"));
		$("#validay").val(_getIntOfJson(0, dt, "ValidDay"));
	}
	
	var dt= _findInfo('../memberOwnService/find/allByLevelId', {'levelId':memberLevelId});
	if(IsNotEmpty(dt)&&dt.state==0){
		if(dt.result.data.length==1){
			$("#theadI").html("tr><th>服务类型</th><th>服务项目</th><th>服务次数</th></tr>");
		}
		
		var htm="";
		for(var i=0;i<dt.result.data.length;i++){
			if(i%2==0){
				
		 htm+="<tr>";
			}
			htm+="<td>"+_getStringOfJson(i, dt,"typeName")+"</td>";
			htm+="<td>"+_getStringOfJson(i, dt,"name")+"</td>";
			var nums = _getIntOfJson(i, dt, "nums");
			if(nums==0){
				nums="无限制";
			}
			htm+="<td>"+nums+"</td>";

			
			if(i%2!=0){
				
			 htm+="</tr>";
				}
		}
		$("#serviceList").html(htm);
		addMargin($("#memberShipWindow"));
	}
}


function loadMemberLevel(shopId) {
	qmsk.initSelect({
		url : '../memberLevel/find/memberLevelList/0/10?shopId=' + shopId,
		id : 'levelId',
		heads : ['ID', 'Name']
	});

}

function preMemberShip(customerId) {
	
	$("#shopId").html($("#shopName").html());
	$("#levelId").empty();
	$("#payPassword").val('');
	$("#memberMemo").val('');

	$("#memberShipWindow").show();
	addMargin($("#memberShipWindow"));
	cache.customerId = customerId;
}

function memberShip() {
	if (qmsk.isEmpty(cache.customerId)) {
		artAlert("请关闭弹出层选择会员入会重试！");
		return;
	}

	var customerId = cache.customerId;

	qmsk.debug = true;

	var ids = ["shopId", "levelId", "payPassword", "memberMemo"];
	var data = qmsk.collectionData(ids);

	if (qmsk.isEmpty(data.shopId)) {
		artAlert("请选择店面");
		return;
	}
	if (qmsk.isEmpty(data.levelId)) {
		artAlert("请选择会员级别");
		return;
	}
	if (qmsk.isEmpty(data.payPassword)) {
		artAlert("请输入支付密码！");
		return;
	}
	qmsk.debug = false;

/*	qmsk.post('../membership/insert/' + data.shopId + '/' + customerId + '/' + data.levelId, data, function(respones) {
*/		
		var dt = _PostInfoRstNoMessage('../membership/insert/' + data.shopId + '/' + customerId + '/' + data.levelId, data);
		if(IsNotEmpty(dt)&&dt.state==0){
			cache.pagination.reload();
			var id = _getIntOfJson(0, dt, "id");
			art.dialog({
				fixed : true,
				lock : true,
				content : "入会成功，是否打印凭证？",
				icon : 'question',
				ok : function() {
					toPrint(id);
				},
				cancel : true
			});
			$("#memberShipWindow").hide();
		}
		
	/*});*/

}

function toPrint(id){
	window.open('printMemberFree.html?id='+id);

}
function toAddPage() {
	qmsk.redirectUrl("ownerCreate.html");
}

function toDetail(customerId, memberCardNo, memberId) {
	qmsk.redirectUrl("owner.html", {
		customerId : customerId,
		memberCardNo : memberCardNo,
		memberId : memberId
	});
}

